package com.shenkh.usunshine.spring.helloworld.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

import com.shenkh.usunshine.spring.helloworld.AbstractGreeting;
import com.shenkh.usunshine.spring.helloworld.AnotherGreeting;
import com.shenkh.usunshine.spring.helloworld.listener.GreetingEvent;


public class HelloWorld extends AbstractGreeting implements AnotherGreeting, MessageSourceAware, ApplicationEventPublisherAware {
	private final static String HELLO_WORLD = "Hello, World!";
	private Logger logger = LoggerFactory.getLogger(HelloWorld.class);
	private MessageSource messageSource;
	private ApplicationEventPublisher applicationEventPublisher;
	private String greetingWords;

	@Override
    public void anotherGreet(String greetingWords) {
		logger.info(greetingWords);
    }
	
	@Override
	protected String getGreetingWords() {
		String message = messageSource.getMessage("helloWorld.message", null, null, null);

		if (StringUtils.isBlank(message)) {
			message = greetingWords;
		}
		
		if (StringUtils.isBlank(message)) {
			message = HELLO_WORLD;
		}
		
		GreetingEvent greetingEvent = new GreetingEvent(this, message);
		applicationEventPublisher.publishEvent(greetingEvent);		
		
		return message;
	}
			
	public void setGreetingWords(String greetingWords) {
		this.greetingWords = greetingWords;
	}	

	@Override
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;		
	}	
	
	@Override
	public void setApplicationEventPublisher(
			ApplicationEventPublisher applicationEventPublisher) {
		this.applicationEventPublisher = applicationEventPublisher;		
	}	
}
